<?

class Selection
{

    var $path;
    var $object;
    var $root;
    var $_root;
    var $_is_dir, $_is_file;

    function Selection($root='',$_root = '') {
        $this->root     = $root;
        $this->_root    = $_root;
        $this->path     = '';
        $this->object   = '';
        $this->_is_dir  = 0;
        $this->_is_file = 0;
    }

    function select($path, $object) {
        $this->path = $path;
        $this->object = $object;
        $this->rel_path = str_replace($this->root,'',$path);
        if ($object != '') {
            if (is_dir($path.$object.'/')) {
                $this->_is_dir  = 1;
                $this->_is_file = 0;
            } elseif (is_file($path.$object)) {
                $this->_is_dir  = 0;
                $this->_is_file = 1;
            }
        } else {
            $this->_is_dir  = 1;
            $this->_is_file = 0;
        }
    }

    function isFileSelected() {
        return $this->_is_file;
    }

    function isDirSelected() {
        return $this->_is_dir;
    }

    function isRootDirSelected() {
        return (!$this->rel_path && !$this->object);
    }

    function getSelected() {
        if ($this->object) {
            if (is_dir($this->path.$this->object)) {
                return new Dir($this->path.$this->object);
            } elseif (is_file($this->path.$this->object)) {
                return new File($this->path.$this->object);
            }
        } else {
            return new Dir($this->path);
        }
    }

    function getCurrentDir() {
        return new Dir($this->path);
    }

    function getCurrentDirName() {
        return $this->path;
    }

    function getCurrentDirNameRel() {
        return $this->rel_path;
    }

    function getParentDirName() {
        $path = $this->path;
        if ($substr($path,-1) == '/') {
            $path = substr($path,0,-1);
        }
        $paths = array_reverse(explode('/',$path));
        unset($paths[0]);
        $path = imlode('/',array_reverse($paths));
        $path .= '/';
        return $path;
    }

    function getParentDirNameRel() {
        $path = $this->path;
        if (substr($path,-1) == '/') {
            $path = substr($path,0,-1);
        }
        $paths = array_reverse(explode('/',$path));
        unset($paths[0]);
        $path = implode('/',array_reverse($paths));
        $path .= '/';
        return str_replace($this->root,'',$path);
    }

}

?>